package apps

import (
	"CampusRecruitment/pkg/apps/ctx"
	"CampusRecruitment/pkg/models"
	"CampusRecruitment/pkg/services"
	"CampusRecruitment/pkg/types"
	"CampusRecruitment/pkg/types/errors"
	"CampusRecruitment/pkg/types/resps"
	"fmt"
)

func CreateNews(c *ctx.Context, form *types.CreateNewsForm) (interface{}, error) {
	user, err := services.GetUserById(c.DB(), c.MustUser().Id)
	if err != nil {
		return nil, err
	}
	news := models.News{
		Title:    form.Title,
		UserId:   user.Id,
		Username: user.Name,
		Content:  form.Content,
		State:    "approve",
	}
	resp, err := services.CreateNews(c.DB(), &news)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func DeleteNewsById(c *ctx.Context, id models.Id) (interface{}, error) {
	news, err := services.GetNewsById(c.DB(), id)
	if err != nil {
		return nil, err
	}
	role, err := CheckLoginUserRole(c)
	if err != nil {
		return nil, err
	}
	fmt.Println(role)
	if c.MustUser().Id != news.UserId && role != "admin" {
		return nil, errors.ErrPermDeny
	}
	deleteErr := services.DeleteNewsById(c.DB(), id)
	if deleteErr != nil {
		return nil, deleteErr
	}
	return resps.DeleteNewsByIdResp{
		Id:     news.Id,
		UserId: news.UserId,
		Title:  news.Title,
	}, nil
}

func UpdateNewsById(c *ctx.Context, form *types.UpdateNewsForm) (interface{}, error) {
	news, err := services.GetNewsById(c.DB(), form.Id)
	if err != nil {
		return nil, err
	}
	if c.MustUser().Id != news.UserId {
		return nil, errors.ErrPermDeny
	}
	updatedNews, updateErr := services.UpdateNews(c.DB(), form)
	if err != nil {
		return nil, updateErr
	}
	return updatedNews, nil
}

func GetNewsList(c *ctx.Context, form *types.GetNewsListForm) (interface{}, error) {
	query := services.GetNewsList(c.DB(), form.Q)
	newsListResp := make([]resps.GetNewsListResp, 0)
	if err := query.Find(&newsListResp).Error; err != nil {
		return nil, errors.AutoDbErr(err)
	}
	return c.PageResult(query, form.Page(), form.PageSize(), &newsListResp)
}

func GetAllNews(c *ctx.Context, form *types.GetNewsListForm) (interface{}, error) {
	role, err := CheckLoginUserRole(c)
	if err != nil {
		return nil, err
	}
	if role != "admin" {
		return nil, errors.ErrPermDeny
	}
	query := services.GetAllNews(c.DB(), form.Q)
	newsListResp := make([]resps.GetNewsListResp, 0)
	if err = query.Find(&newsListResp).Error; err != nil {
		return nil, errors.AutoDbErr(err)
	}
	return c.PageResult(query, form.Page(), form.PageSize(), &newsListResp)
}

func NewsApprove(c *ctx.Context, id models.Id) (interface{}, error) {
	role, err := CheckLoginUserRole(c)
	if err != nil {
		return nil, err
	}
	if role != "admin" {
		return nil, errors.ErrPermDeny
	}
	if err = services.UpdateNewsStateById(c.DB(), id, "active"); err != nil {
		return nil, errors.AutoDbErr(err)
	}
	return nil, nil
}

func CloseNews(c *ctx.Context, id models.Id) (interface{}, error) {
	news, err := services.GetNewsById(c.DB(), id)
	if err != nil {
		return nil, err
	}
	role, err := CheckLoginUserRole(c)
	if err != nil {
		return nil, err
	}
	if role != "admin" && news.UserId != c.MustUser().Id {
		return nil, errors.ErrPermDeny
	}
	if err = services.UpdateNewsStateById(c.DB(), id, "inactive"); err != nil {
		return nil, errors.AutoDbErr(err)
	}
	return nil, nil
}

func OpenNews(c *ctx.Context, id models.Id) (interface{}, error) {
	news, err := services.GetNewsById(c.DB(), id)
	if err != nil {
		return nil, err
	}
	if news.UserId != c.MustUser().Id {
		return nil, errors.ErrPermDeny
	}
	if err = services.UpdateNewsStateById(c.DB(), id, "approve"); err != nil {
		return nil, errors.AutoDbErr(err)
	}
	return nil, nil
}

func GetNewsContent(c *ctx.Context, id models.Id) (interface{}, error) {
	news, err := services.GetNewsById(c.DB(), id)
	if err != nil {
		return nil, err
	}
	return news, nil
}

func GetMyNews(c *ctx.Context, form *types.GetMyNewsForm) (interface{}, error) {
	form.Id = c.MustUser().Id
	query := services.GetMyNewsByUserId(c.DB(), form)
	newsListResp := make([]resps.GetNewsListResp, 0)
	if err := query.Find(&newsListResp).Error; err != nil {
		return nil, errors.AutoDbErr(err)
	}
	return c.PageResult(query, form.Page(), form.PageSize(), &newsListResp)
}
